package com.example.review.controller;

import com.example.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ReviewController {

    @Autowired
    public ReviewService reviewService;

    @GetMapping("/firstDay")
    public String firstDay() {
        try {
            return getTheNum1();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<Integer> getTheNum() {
        Integer[] intArr = {2, 1, 3, 5, 4, 18, 9, 14, 13, 10};
        try {
            return reviewService.getTheNum(intArr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public String getTheNum1() {
        Integer[] intArr = {2, 1, 3, 5, 4, 18, 9, 14, 13, 10};
        try {
            return reviewService.getTheNum1(intArr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // String 、 StringBuilder 、 StringBuffer 拼接耗时
    public void StringPerformanceTest() {
        int n = 100000; // 操作次数

        // 使用 String
        long startTime = System.currentTimeMillis();
        String str = "";
        for (int i = 0; i < n; i++) {
            str += i;
        }
        long endTime = System.currentTimeMillis();
        System.out.println("String 耗时: " + (endTime - startTime) + "ms");

        // 使用 StringBuilder
        startTime = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuilder 耗时: " + (endTime - startTime) + "ms");

        // 使用 StringBuffer
        startTime = System.currentTimeMillis();
        StringBuffer sbf = new StringBuffer();
        for (int i = 0; i < n; i++) {
            sbf.append(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer 耗时: " + (endTime - startTime) + "ms");
    }
}
